/**
 * 
 */
package fr.ants.view.panel;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ScrollPaneConstants;

import fr.ants.controller.clock.Clock;
import fr.ants.controller.command.NewNestCommand;
import fr.ants.controller.command.NewObstacleCommand;
import fr.ants.controller.command.NewResourceCommand;
import fr.ants.controller.draganddrop.ButtonTransferHandler;
import fr.ants.controller.listener.ButtonsListener;
import fr.ants.controller.listener.CanvasListener;

/**
 * It's the main panel for the the frame. It contain all components
 * 
 * @author Nyu
 * @generated 
 *            "UML vers Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class MainPanel extends JPanel {
	/**
	 * Serial UID.
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * control panel.
	 */
	private final ControlPanel controlPanel;

	/**
	 * tabbed pane.
	 */
	private final TabbedPane tabbedPane;

	/**
	 * canvas.
	 */
	private final Canvas canvas;

	/**
	 * Build the panel.
	 * 
	 * @param buttonsListener listener for the buttons
	 * @param clock clock
	 */
	public MainPanel(final ButtonsListener buttonsListener, final Clock clock) {
		setLayout(new BorderLayout());

		// Canvas
		canvas = new Canvas();
		controlPanel = new ControlPanel(buttonsListener);
		// to know if the canvas have changed
		canvas.addPropertyChangeListener(controlPanel.getControlButtonsPanel());
		// to know if the state of clock have changed
		clock.addPropertyChangeListener(controlPanel.getControlButtonsPanel());

		// Commands
		final NewNestCommand newNestCommand = new NewNestCommand(canvas, clock);
		final NewResourceCommand newResourceCommand = new NewResourceCommand(
				canvas, clock);
		final NewObstacleCommand newObstacleCommand = new NewObstacleCommand(
				canvas, clock);
		// the listener use these commands
		buttonsListener.setNewObstacleCommand(newObstacleCommand);
		buttonsListener.setNewResourceCommand(newResourceCommand);

		// for the drag and drop
		final ButtonTransferHandler transferHandler = new ButtonTransferHandler(
				newNestCommand, newResourceCommand, newObstacleCommand);
		// to inform the canvas what to do when it receive something by drag and
		// drop
		canvas.setTransferHandler(transferHandler);

		// TabbedPane
		tabbedPane = new TabbedPane(canvas, transferHandler, buttonsListener);

		// These commands
		newNestCommand.setNestPanel(tabbedPane.getNestPanel());
		newResourceCommand.setTabbedPane(tabbedPane);
		newObstacleCommand.setTabbedPane(tabbedPane);

		// Listener for the canvas
		final CanvasListener cListener = new CanvasListener(buttonsListener,
				clock, canvas, newObstacleCommand, newResourceCommand,
				newNestCommand);
		canvas.addMouseListener(cListener);
		canvas.addMouseMotionListener(cListener);
		canvas.addPropertyChangeListener(tabbedPane);

		// split the panel horizontally to have the top menu that contain the
		// control and on the bottom panel the tabbedPane
		final JSplitPane leftSplitPane = new JSplitPane(
				JSplitPane.VERTICAL_SPLIT, controlPanel, tabbedPane);

		// split the panel vertically to have the menu on the left and the
		// canvas on the rights
		final JSplitPane rightSplitPane = new JSplitPane(
				JSplitPane.HORIZONTAL_SPLIT, leftSplitPane, new JScrollPane(
						canvas, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
						ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS));

		add(rightSplitPane, BorderLayout.CENTER);
	}

	/**
	 * 
	 * @return the canvas
	 */
	public Canvas getCanvas() {
		return canvas;
	}
}